﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
using BISPOS.Repository;
namespace BISPOS
{
    public partial class AddNewYearStockXtraForm : DevExpress.XtraEditors.XtraForm
    {
        private ProductRepository productRepository = new ProductRepository();
        private List<AddNewYearStockGridItem> items = new List<AddNewYearStockGridItem>();
        private Product _product;
        public AddNewYearStockXtraForm()
        {
            InitializeComponent();

            this.Text = "ยอดสินค้าคงเหลือยกมา (1 เม.ย. " + (DateTime.Now.Year + 543 - 1).ToString() + " - 31 มี.ค. " + (DateTime.Now.Year + 543).ToString() + ")";
        }

        private void barcodeTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    Product product = productRepository.Get(barcodeTextEdit.Text.Trim());
                    _product = product;
                    this.productTextEdit.Text = product.Name;
                    this.unitTextEdit.Text = product.Unit;

                    this.sellTextEdit.Focus();

                }
                catch
                {
                    MessageBox.Show("ไม่พบสินค้า", "รหัสผิดพลาด", MessageBoxButtons.OK);
                    ClearProductForm();
                }
            }
        }

        public void ClearProductForm()
        {
            barcodeTextEdit.ResetText();
            barcodeTextEdit.Focus();

            productTextEdit.ResetText();
            unitTextEdit.ResetText();
            sellTextEdit.ResetText();
            capitalTextEdit.ResetText();
            quantityTextEdit.ResetText();
            sumCapitalTextEdit.ResetText();
            sumSellTextEdit.ResetText();

            _product = null;
        }

        private void quantityTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {

                sumSellTextEdit.Text = (Convert.ToDecimal(quantityTextEdit.Text) * Convert.ToDecimal(sellTextEdit.Text)).ToString("N2");
                sumCapitalTextEdit.Text = (Convert.ToDecimal(quantityTextEdit.Text) * Convert.ToDecimal(capitalTextEdit.Text)).ToString("N2");
            }
            catch { quantityTextEdit.ResetText(); }
        }

        private void quantityTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DialogResult result = MessageBox.Show("เพิ่ม", "xx", MessageBoxButtons.OKCancel);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    items.Add(new AddNewYearStockGridItem
                    {
                        Id = _product.Id.ToString(),
                        Barcode = _product.Barcode,
                        Name = _product.Name,
                        CapitalPrice = Convert.ToDecimal(capitalTextEdit.Text),
                        Quantity = Convert.ToDouble(quantityTextEdit.Text),
                        SellPrice = Convert.ToDecimal(sellTextEdit.Text),
                        SumCapitalPrice = Convert.ToDecimal(sumCapitalTextEdit.Text),
                        SumSellPrice = Convert.ToDecimal(sumSellTextEdit.Text)
                    });
                    linqServerModeSource1.QueryableSource = items.AsQueryable();
                    ClearProductForm();
                }
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            AddNewYearStockGridItem item = (AddNewYearStockGridItem)gridView1.GetRow(e.RowHandle);
            DialogResult result = MessageBox.Show("ลบรายการ: " + item.Barcode + " - " + item.Name, "ลบ", MessageBoxButtons.OKCancel);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                items.Remove(item);
                linqServerModeSource1.QueryableSource = items.AsQueryable();
                ClearProductForm();
            }

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            BISPOSDataClassesDataContext db= new BISPOSDataClassesDataContext();
            Bill bill = new Bill();
            if (db.Bills.Count() > 0)
                    bill = db.Bills.FirstOrDefault();
            foreach (AddNewYearStockGridItem item in linqServerModeSource1.QueryableSource)
            {
                BillItem billItem = new BillItem{
                    Stock = new Stock{
                        BuyingCost = item.SumCapitalPrice,
                        CurrentQuantity = item.Quantity,
                        OriginalQuantity = item.Quantity,
                        ProductId = Convert.ToInt32(item.Id),
                        Date = DateTime.Now.Date}
                };
                bill.BillItems.Add(billItem);
            }


            if (db.Bills.Count() > 0)
            {
                db.SubmitChanges();
            }
            else
            {
                bill.Date = DateTime.Now.Date;
                bill.IsCredit = false;
                bill.Note = this.Text;
                db.Bills.InsertOnSubmit(bill);
                db.SubmitChanges();
            
            }
            MessageBox.Show("ทำรายการเรียบร้อย", "Success", MessageBoxButtons.OK);
            this.Close();
        }

        private void sellTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                capitalTextEdit.Focus();
        }

        private void capitalTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                quantityTextEdit.Focus();
        }
    }
}